package cn.edu.suda.catalina.health;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.support.v7.view.ContextThemeWrapper;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.Date;

import cn.edu.suda.catalina.entity.Diary;
import cn.edu.suda.catalina.util.MSQLiteService;

public class DiaryFragment extends DialogFragment {

    private OnDiaryFragmentInteractionListener mListener;

    public DiaryFragment() {
    }

    public static DiaryFragment newInstance() {
        return new DiaryFragment();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        LayoutInflater inflater = getActivity().getLayoutInflater();
        View rootView = inflater.inflate(R.layout.fragment_diary, null);

        final TextView timeText = (TextView) rootView.findViewById(R.id.diary_time);
        timeText.setText(getCurrentTime());

        final EditText diaryText = (EditText) rootView.findViewById(R.id.diary_diary);

        AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(getActivity(), R.style.AlertDialogTheme));
        builder.setView(rootView).setPositiveButton("保存", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (!TextUtils.isEmpty(diaryText.getText())) {
                    insert(timeText.getText().toString(), diaryText.getText().toString());
                    onButtonPressed();
                }
            }
        }).setNegativeButton("取消", null);
        return builder.create();
    }

    private String getCurrentTime() {

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日");
        Date currentDate = new Date(System.currentTimeMillis());
        return formatter.format(currentDate);
    }

    private void insert(String timeText, String diaryText) {
        Diary diary = new Diary();
        diary.setTime(timeText);
        diary.setDiary(diaryText);
        new MSQLiteService(getActivity()).insert(diary);
    }

    public void onButtonPressed() {
        if (mListener != null) {
            mListener.onDiaryFragmentInteraction();
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (activity instanceof OnDiaryFragmentInteractionListener) {
            mListener = (OnDiaryFragmentInteractionListener) activity;
        } else {
            throw new RuntimeException(activity.toString()
                    + " must implement OnDiaryFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    public interface OnDiaryFragmentInteractionListener {
        void onDiaryFragmentInteraction();
    }
}
